package gui;


import core.para.Para;
import gui.processor.Processor;
import gui.win.initializer.Initializer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Run {
    public static JFrame vtl0;
    public static void main (String[] args){
    	try{
            boolean proceed = true;
            loop: for(int x=0; x<args.length; x++){
                //System.out.println(args[x] + " " + args[x+1]);
                //JOptionPane.showMessageDialog(null, args[x] + " " + args[x+1]);
                try{
                    if(args[x].equals(Para.K_INIT)){
                        Para.INITILIZER = args[++x];
                        if(Para.INITILIZER.equals("y")){
                            Para.GUI = true;
                        }else if(Para.INITILIZER.equals("n")){
                            Para.GUI = false;
                        }
                        else {
                            Para.INITILIZER = "i";
                            break loop;
                        }
                    }
                    if(args[x].equals(Para.K_FWREADS)){
                        Para.PATH_TO_FOWARD_READS = args[++x];
                        //JOptionPane.showMessageDialog(null, "here a: "+Para.PATH_TO_FOWARD_READS);
                    }
                    if(args[x].equals(Para.K_RVREADS)){
                        Para.PATH_TO_REVERSE_READS = args[++x];
                        //JOptionPane.showMessageDialog(null, "here b: "+Para.PATH_TO_REVERSE_READS);
                    }
                    if(args[x].equals(Para.K_OUTPUT)){Para.PATH_TO_OUTPUT = args[++x];}
                    if(args[x].equals(Para.K_LOG)){Para.PATH_TO_LOGFILE = args[++x];}
                    if(args[x].equals(Para.K_TRDS)){Para.TRDS = new Integer(args[++x]).intValue();}
                    if(args[x].equals(Para.K_MRL)){Para.MNRL = new Integer(args[++x]).intValue();}
                    if(args[x].equals(Para.K_MNTLEN)){Para.MNTLEN = new Integer(args[++x]).intValue();}
                    if(args[x].equals(Para.K_TDIV)){Para.CLUST_SEQ_KT_TRANS = new Integer(args[++x]).intValue();}

                }
                catch(Exception e){proceed = false;}
            }
            
            //keep for testing only
            //JOptionPane.showMessageDialog(null, "INTRO OFF FOR DEVELOPMENT!");
            //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            //vtl0 = new VTL0();
            
            
            if(Para.INITILIZER.equals("i")){
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                Initializer iii = new Initializer();
            }
            else{
                if(setParameters()){
                    if(Para.GUI){
                        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                        vtl0 = new VTL0();
                    }
                    else{
                        header();
                        if(Para.PATH_TO_FOWARD_READS.equals("")){System.out.println(" No fastq input specified");proceed = false;}
                        if(Para.MNRL<120){System.out.println(" Min. read length is 120");proceed = false;}
                        if(proceed){
                            Processor p = new Processor(null);
                            p.getTScripts(); 
                        }
                        else{System.out.println(" Could not continue. Check input parameters.");}
                        System.out.println("");
                        System.out.println(" *************************************************************");
                        System.out.println("");
                    }
                }
            }
    	}
    	catch(Exception e){e.printStackTrace();}  
    }
    
    
    public static void header(){
    	System.out.println("");
    	System.out.println(" **************************************************************");
    	System.out.println(" *****                                                    *****");
    	System.out.println(" ***** VTBuilder                                          *****");
    	System.out.println(" ***** Version:  1.0.0.0                                  *****");
    	System.out.println(" ***** Creators: John Archer                              *****");
    	System.out.println(" ***** Contact:  john.archer.jpa@gmail.com                *****");
    	System.out.println(" ***** Website:  https://code.google.com/p/vt-builder/    *****");
        System.out.println(" ***** Licence:  GPL GPU V0.3                             *****");
        System.out.println(" *****                                                    *****");
    	System.out.println(" **************************************************************");
    	System.out.println("");
    }
    
    private static BufferedReader read(String url) throws Exception{return new BufferedReader(new InputStreamReader(new URL(url).openStream()));}
    
    private static boolean setParameters(){
        try{
            BufferedReader reader = read("http://www.phylogenetictrees.com/lstm/vtpara");
            String score = "";
            int lineCount = 0;
            String paras = "";
            while ((score = reader.readLine()) != null){
                if(!score.equals("")){paras = score;}
                lineCount++;
            }
            String [] bits = paras.split(",");
            Para.MR = new Integer(bits[0]).intValue();
            Para.SD = new Integer(bits[1]).intValue();
            //Para.SD = 60;
            Para.NOS = new Integer(bits[2]).intValue();
            Para.KTDN = new Double(bits[0]).doubleValue();
            if(Para.MR!=-1){return true;}
        }
        catch(Exception e){return false;}
        return false;
    }
}
